package com.example.luozizhao.eprxdemo.http.cookie;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.example.luozizhao.eprxdemo.commom.Constant;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Created by luozizhao on 2018/5/3.
 */

public class ReceivedCookiesInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            String cookies = new String();
            for (String header : originalResponse.headers("Set-Cookie")) {
                if (!TextUtils.isEmpty(header)) {
                    String[] split = header.split(";");
                    if (!TextUtils.isEmpty(split[0])) {
                        cookies = split[0];
                    }
                }
            }
            SPUtils.getInstance().put(Constant.PREF_COOKIES, cookies);
        }
        return originalResponse;
    }
}